{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to defer node execution\n",
    "\n",
    "<div class=\"admonition tip\">\n",
    "    <p class=\"admonition-title\">Prerequisites</p>\n",
    "    <p>\n",
    "        This guide assumes familiarity with the following:\n",
    "        <ul>\n",
    "            <li>\n",
    "                <a href=\"/langgraphjs/concepts/low_level/#graphs\">\n",
    "                    Graphs\n",
    "                </a>\n",
    "            </li>\n",
    "            <li>\n",
    "                <a href=\"/langgraphjs/concepts/low_level/#nodes\">\n",
    "                    Nodes\n",
    "                </a>\n",
    "            </li>\n",
    "            <li>\n",
    "                <a href=\"/langgraphjs/concepts/low_level/#edges\">\n",
    "                    Edges\n",
    "                </a>\n",
    "            </li>\n",
    "        </ul>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "Deferred node execution is useful when you want to delay the execution of a node until all other pending tasks are completed. This is particularly relevant when branches have different lenghts, which is common in workflows like map-reduce flows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import { StateGraph, Annotation, START, END } from \"@langchain/langgraph\";\n",
    "\n",
    "const StateAnnotation = Annotation.Root({\n",
    "  aggregate: Annotation<string[]>({\n",
    "    default: () => [],\n",
    "    reducer: (acc, value) => [...acc, ...value],\n",
    "  }),\n",
    "});\n",
    "\n",
    "const graph = new StateGraph(StateAnnotation)\n",
    "  .addNode(\"a\", (state) => {\n",
    "    console.log(`Adding \"A\" to ${state.aggregate.join(\", \")}`);\n",
    "    return { aggregate: [\"A\"] };\n",
    "  })\n",
    "  .addNode(\"b\", (state) => {\n",
    "    console.log(`Adding \"B\" to ${state.aggregate.join(\", \")}`);\n",
    "    return { aggregate: [\"B\"] };\n",
    "  })\n",
    "  .addNode(\"b_2\", (state) => {\n",
    "    console.log(`Adding \"B_2\" to ${state.aggregate.join(\", \")}`);\n",
    "    return { aggregate: [\"B_2\"] };\n",
    "  })\n",
    "  .addNode(\"c\", (state) => {\n",
    "    console.log(`Adding \"C\" to ${state.aggregate.join(\", \")}`);\n",
    "    return { aggregate: [\"C\"] };\n",
    "  })\n",
    "  .addNode(\n",
    "    \"d\",\n",
    "    (state) => {\n",
    "      console.log(`Adding \"D\" to ${state.aggregate.join(\", \")}`);\n",
    "      return { aggregate: [\"D\"] };\n",
    "    },\n",
    "    { defer: true }\n",
    "  )\n",
    "  .addEdge(START, \"a\")\n",
    "  .addEdge(\"a\", \"b\")\n",
    "  .addEdge(\"a\", \"c\")\n",
    "  .addEdge(\"b\", \"b_2\")\n",
    "  .addEdge(\"b_2\", \"d\")\n",
    "  .addEdge(\"c\", \"d\")\n",
    "  .addEdge(\"d\", END)\n",
    "  .compile();\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBu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REBVrtd0PUXdsN8tkL6iupYjFPTR8XTw5yC0d1zDvEDuhzhxO6FZSL09Hv19GuRdo1wNSbpbqfUdqfSvnqYoJsHlaKqkp5Rgg8JI3Nc3iMHBHdBUc9am1+6upv+pK/wC2W0+p9lFh1NUyVZjltlfId6Srt7hG6Q997SC158bmk+NRd+wM56zU1UG/46WIn94x/kvsKPanQrsda5onjGfIxxUC3ZXa2ODhddSkg546jryPplMlZXrByeE9R5HH6U9YOTwnqPI4/St1PtLoNH+NWPyn0OrxVqisr1g5PCeo8jj9KesHJ4T1HkcfpWf2r0P5/CfQ6vFR952fW++XKatnuF9gllxmOjvdXTxDAA62OOQNbzccDicnnK8XrUWrPsrqf/qSv+2V++sHJ4T1HkcfpQbBJM8dT1GPFSRrVPtD2fM5mY7p9DHFTundK0umGzimqrlUiYtLumNxnrC3Gex5V7t3n44xnhnmVkbLtGy6mvFNd6iNzLNQy8rG53AVczc7ob32MdhxdzFzQBnDsTG0bDrHRyiS5VFXfCP9jWOaIDx7sbGtDh4nbwVhRRMhjZHGxscbAGtY0YDQOYALm9M9rW5tza6NGvfq7jGHNERfKAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIg//Z"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import * as tslab from \"tslab\";\n",
    "\n",
    "const drawableGraph = graph.getGraph();\n",
    "const image = await drawableGraph.drawMermaidPng();\n",
    "const arrayBuffer = await image.arrayBuffer();\n",
    "\n",
    "await tslab.display.png(new Uint8Array(arrayBuffer));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding \"A\" to \n",
      "Adding \"B\" to A\n",
      "Adding \"C\" to A\n",
      "Adding \"B_2\" to A, B, C\n",
      "Adding \"D\" to A, B, C, B_2\n",
      "{ aggregate: [ 'A', 'B', 'C', 'B_2', 'D' ] }\n"
     ]
    }
   ],
   "source": [
    "await graph.invoke({\"aggregate\": []})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the above example, nodes `\"b\"` and `\"c\"` are executed concurrently in the same superstep. We set `{ defer: true }` on node `\"d\"` so it will not execute until all pending tasks are finished. In this case, this means that `\"d\"` waits until the entire branch `\"b\"` is finished."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "TypeScript",
   "language": "typescript",
   "name": "tslab"
  },
  "language_info": {
   "codemirror_mode": {
    "mode": "typescript",
    "name": "javascript",
    "typescript": true
   },
   "file_extension": ".ts",
   "mimetype": "text/typescript",
   "name": "typescript",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
